<?
class ZPlus_Mysql
{
  private $conn = null;

  public function connect($host, $user, $pass, $name)
  {
    $c = mysql_connect($host, $user, $pass);
    if (!$c) return false;
    if (!mysql_select_db($name, $c)) return false;
    $this->conn = $c;
    return true;
  }
  
  public function connectByConfig($handle, $cfg)
  {
    $all = parse_ini_file($cfg, true);
    $config = $all['production'];

    $host = $config["zenddb.$handle.params.host"];
    $name   = $config["zenddb.$handle.params.dbname"];
    $user   = $config["zenddb.$handle.params.username"];
    $pass   = $config["zenddb.$handle.params.password"];

    return $this->connect($host, $user, $pass, $name);
  }
  
  public function close()
  {
    mysql_close($this->conn);
    $this->conn = null;
  }
  
  public function fetchAll($sql)
  {
    $res = mysql_query($sql, $this->conn);
    if (!$res) return false;
    $ret = array();
    while ($r = mysql_fetch_assoc($res))
      $ret[] = $r;
    return $ret;
  }

  public function fetchRow($sql)
  {
    $res = mysql_query($sql, $this->conn);
    if (!$res) return false;
    if ($r = mysql_fetch_assoc($res))
      return $r;
    return false;
  }

  public function fetchOne($sql)
  {
    $res = mysql_query($sql, $this->conn);
    if (!$res) return false;
    if ($r = mysql_fetch_row($res))
      return $r[0];
    return false;
  }

  public function execute($sql)
  {
    return mysql_query($sql, $this->conn);
  }
}
